﻿#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   Promote machine to be primary Domain Controller
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

Set-StrictMode -Version Latest

if ((Get-Module MDE-Common) -eq $null) {
    Import-Module MDE-Common
    }

[string]$executionPath = Split-Path $script:MyInvocation.MyCommand.Path -Parent

Function InstallProduct() {

    SetIpConfiguration  MDE_DC_STATIC_IP MDE_STATIC_GATEWAY MDE_SUBNET_MASK
    Install-ServerFeatures "$executionPath\ServerFeatures.csv"    
    PromoteDC
    VerifyTasks
    }
    
#region Framework

Function GetInstallName() {
    return "Mi-DevEnv Domain Controller"
    }

Function GetInstalled() {
    return (Get-FeatureInstalled ADDS-Domain-Controller)
    }

Function GetPackageName() {
    return "DC2008R2"
    }

Function GetPackageDescription() {
    return "Build 2008R2 Domain Controller"
    }
    
Function GetServerBuild() {
    return $true
    }

Function GetInstallPriority() {
    return 1000
    }

Function GetRequireRestart() {
    return $true
    }

#endregion

#region Tasks

Function SetIpConfiguration([string]$dcStaticIp,[string]$gatewayIp,[string]$subnetMask) {

    $ipConfig = Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE

    if ( !($ipConfig.IPAddress -contains "$dcStaticIp") ) {

        Write-Progress "PROCESS START : Build Domain Controller"

        Write-Progress "Verifying IP settings and connectivity"

        if ((Test-Connection $dcStaticIp -Count 1 -Quiet) -eq $true) {
            throw "There is already a machine on the network with IP address $dcStaticIp. Edit the parameters file and change the static IP address assigned to the domain controller."
            }

        if (!(Test-Connection $gatewayIp -Quiet -Count 1)) {
            Write-Progress "WARNING: Failed to ping the default gateway $gatewayIp"
            }

        Write-Progress "Assigning static IP address : $dcStaticIp"
            
        $ipConfig.EnableStatic("$dcStaticIp", $subnetMask) > $null
        $ipConfig.SetGateways($gatewayIp, 1) > $null
        #$ipConfig.SetDNSServerSearchOrder($dcStaticIp) > $null

        Write-Progress "Static IP address set : $dcStaticIp"
        }
    }

Function PromoteDC() {
    Write-Progress "Promoting machine to domain controller with DCPromo"
    & $env:windir\system32\dcpromo.exe /Answer:"$executionPath\ResponseDcPromo.ini"
    Write-Progress "Machine promoted to DC"
    }

Function VerifyTasks() {

    $completeJobs = 0
    $totalJobs = 2

    $computerName = Get-ComputerName
    $isDC = Get-FeatureInstalled ADDS-Domain-Controller
    $ipConfig = Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE
   
    if ($ipConfig.DefaultIPGateway -eq "MDE_STATIC_GATEWAY") {
        $completeJobs ++
        Write-Progress "VERIFIED : IP Configuration"
        }
    else {
        Write-Progress "FAILED : IP Configuration"
        }

    if ($isDC) {
        $completeJobs ++
        Write-Progress "VERIFIED : Domain controller setup"
    }
    else {
        Write-Progress "FAILED : Domain controller setup"
    }

    if ($completeJobs -ne $totalJobs) {
        Write-Progress "PROCESS FAILED   : Build Domain Controller"
        }
    else {
        Write-Progress "PROCESS COMPLETE : Build Domain Controller"
        }
    }

#endregion